﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using MarketOnlineDTO;

namespace MarketOnlineDAO
{
    public class AdminDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        public List<AdminDTO> layDanhSachAdmin() //*sai tên cột // admin phai ko, da sua
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from admin";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<AdminDTO> list = new List<AdminDTO>();

            foreach (DataRow row in dt.Rows)
            {
                AdminDTO dto = new AdminDTO();
                dto.MaAdmin = int.Parse(row["MaAD"].ToString());
                dto.UserName = row["UserName"].ToString();
                dto.PassWord = row["PassWord"].ToString();
                dto.HoTen = row["HoTen"].ToString();
                dto.CMND = row["CMND"].ToString();
                dto.DiaChi = row["DiaChi"].ToString();
                dto.DienThoai = row["DienThoai"].ToString();
                dto.Email = row["Email"].ToString();
                list.Add(dto);
            }

            return list;

        }

      /*  public AdminDTO layAdmin(int MaAdmin)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from admin where MaAdmin=" + MaAdmin;
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            AdminDTO adMindto;

            foreach (DataRow row in dt.Rows)
            {

                adMindto = new AdminDTO();
                adMindto.MaAdmin = int.Parse(row("MaAdmin").ToString());
                adMindto.UserName = row["UserName"].ToString();
                adMindto.PassWord = row["PassWord"].ToString();
                adMindto.HoTen = row["HoTen"].ToString();
                adMindto.CMND = row["CMND"].ToString();
                adMindto.DiaChi = row["DiaChi"].ToString();
                adMindto.DienThoai = row["DienThoai"].ToString();
                adMindto.Email = row["Email"].ToString();
                //list.Add(adMindto);
            }

            return adMindto;

        } */
        public AdminDTO layAdmin(String Username,String Password) //* ko rõ công dụng của hàm này... // dung de dang nhap
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from Admin where Username= @Username and Password= @Password";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@Username", Username));
            _cmd.Parameters.Add(new MySqlParameter("@Password", Password));


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            AdminDTO ad = new AdminDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

               
                ad.MaAdmin = int.Parse(row["MaAD"].ToString());
                ad.UserName = row["UserName"].ToString();
                ad.PassWord = row["PassWord"].ToString();
                ad.HoTen = row["HoTen"].ToString();
                ad.CMND = row["CMND"].ToString();
                ad.DiaChi = row["DiaChi"].ToString();
                ad.DienThoai = row["DienThoai"].ToString();
                ad.Email = row["Email"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ad;
        }

        public Boolean themAdmin(AdminDTO dto)//*sai tên cột // admin phai ko, da sua
        {
            _cmd = CreateMysqlCommand();
          //  _cmd.CommandText = "insert into Admin value ('" + dto.UserName + "','" + dto.PassWord + "','" + dto.HoTen + "','" + dto.CMND + "','" + dto.DiaChi + "','" + dto.DienThoai + "','" + dto.Email + " ')";
            _cmd.CommandText = "insert into Admin value (@UserName,@PassWord,@HoTen,@CMND,@DiaChi,@DienThoai,@Email)";
            _cmd.CommandType = CommandType.Text;
            //Dua cac bien vao 
            _cmd.Parameters.Add(new MySqlParameter("@UserName",dto.UserName)) ;
            _cmd.Parameters.Add(new MySqlParameter("@PassWord",dto.PassWord)) ;
             _cmd.Parameters.Add(new MySqlParameter("@HoTen",dto.HoTen) );
             _cmd.Parameters.Add(new MySqlParameter("@CMND",dto.CMND)) ;
            _cmd.Parameters.Add(new MySqlParameter("@DiaChi",dto.DiaChi)) ;
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai",dto.DienThoai) );
             _cmd.Parameters.Add(new MySqlParameter("@Email",dto.Email)) ;

            return ExecuteNonQuery(_cmd);

        }

        public Boolean capNhatAdmin(AdminDTO dto) //*sai tên cột // admin phai ko, da sua
      
        {
            _cmd = CreateMysqlCommand();
           // _cmd.CommandText = "update Admin set UserName='" + dto.UserName + "',Password='" + dto.PassWord + "',HoTen='" + dto.HoTen + "',CMND='" + dto.CMND + "',DiaChi='" + dto.DiaChi + "',DienThoai='" + dto.DienThoai + "',Email='" + dto.Email + " ' where MaAdmin='" + dto.MaAdmin + "')";
            _cmd.CommandText = "update Admin set UserName=@UserName,Password=@PassWord,HoTen=@HoTen,CMND=@CMND,DiaChi=@DiaChi,DienThoai=@DienThoai,Email=@Email where MaAD=@MaAD)" ;
            _cmd.CommandType = CommandType.Text;
               _cmd.Parameters.Add(new MySqlParameter("@UserName",dto.UserName)) ;
            _cmd.Parameters.Add(new MySqlParameter("@PassWord",dto.PassWord)) ;
             _cmd.Parameters.Add(new MySqlParameter("@HoTen",dto.HoTen)) ;
             _cmd.Parameters.Add(new MySqlParameter("@CMND",dto.CMND)) ;
            _cmd.Parameters.Add(new MySqlParameter("@DiaChi",dto.DiaChi)) ;
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai",dto.DienThoai)) ;
             _cmd.Parameters.Add(new MySqlParameter("@Email",dto.Email)) ;
             _cmd.Parameters.Add(new MySqlParameter("@MaAD", dto.MaAdmin));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean xoaAdmin(int MaAdmin) //*sai tên cột // admin phai ko, da sua
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete Admin where MaAD = " + MaAdmin + " ";
            _cmd.CommandType = CommandType.Text;

            return ExecuteNonQuery(_cmd);
        }
    }
}
